package cn.yunhe.dao;

import cn.yunhe.entity.UserInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

@Mapper
public interface UserDao {

    @Select("select * from users where userName =#{username}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "roles", javaType = List.class, many =
            @Many(select = "cn.yunhe.dao.RoleDao.selectRoleByUserId", fetchType = FetchType.LAZY), column = "id")
    })
    UserInfo findUserByUserName(String s);

    @Select("select * from users")
    List<UserInfo> findAll();

    @Insert("insert into users values (null, #{email},#{username}, #{password}, #{phoneNum}, #{status})")
    void insertUser(UserInfo userInfo);

    @Select("select username from users where username =#{username}")
    int findUserName(String username);

    @Delete("delete from users where id = #{userId}")
    void deleteUserById(Integer userId);

    @Delete("delete  from users_role where userId = #{userId}")
    void deleteUrByUserId(Integer userId);

    @Select("select *  from users where id = #{id}")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(property = "roles", javaType = List.class, many =
            @Many(select = "cn.yunhe.dao.RoleDao.selectRole", fetchType = FetchType.LAZY), column = "id")
    })
    UserInfo findUserByIdAndAllRole(Integer id);

    @Select("select * from users where id = #{id}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "roles", javaType = List.class, many = @Many(select = "cn.yunhe.dao.RoleDao.findById"), column = "id")
    })
    UserInfo findById(Integer id);

    @Select("select * from users where username like '%' #{userName} '%' ")
    List<UserInfo> selectUserByLike(String userName);

}
